package com.andnnl.stack1;

import java.util.Stack;

/**
 * Created by chenss on 2023/7/10.
 */
public class SpelTest {
    public static void main(String[] args) {
        String str = "3+2*2";
        Stack<String> valStack = new Stack();
        Stack<String> operStack = new Stack();
        String pattenNumber = "[0-9]";
        String pattenOperate = "[/\\*\\+-]";
        String operateLv2 = "[\\+-]";

        String lastOperate = "";
        String number = "";
        for (String s : str.split("")) {
            System.out.println(s);
            if (s.matches(pattenOperate)) {
                if (!"".equals(lastOperate) && !"".equals(number)) {
                    cal(valStack, lastOperate,s, number);
                    lastOperate = "";
                    number = "";
                    continue;
                } else if (!"".equals(number)) {
                    valStack.push(number);
                    number="";
                }
                lastOperate = s;
            } else if (s.matches(pattenNumber)) {
                number += s;
            }

//                valStack.push(s);
        }

    }


    private static void cal(Stack<String> stack, String lastOperate,  String curOperate, String temp) {
//        while (stack.peek() != null) {
//            if(!temp.equals("")){
//
//            }
        int x = Integer.parseInt(stack.pop());
        int y = Integer.parseInt(temp);
            int rs = 0;
            if (lastOperate.equals("*")) {
                rs = x * y;
            } else if (lastOperate.equals("/")) {
                rs = x / y;
            } else if (lastOperate.equals("+")) {
                rs = x + y;
            } else if (lastOperate.equals("-")) {
                rs = x - y;
            } else if (lastOperate.equals(" ")) {
            }

            System.out.println(x +" "+lastOperate+" "+y+" = "+rs);
            stack.push("" + rs);
//        }
    }
}
